package com.tpp.admin.controller;

import com.tpp.admin.service.MenuService;
import com.tpp.comm.pojo.TpMenu;
import com.tpp.comm.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/s/menu")
@Slf4j
public class MenuController {

    @Resource
    private MenuService menuService;

    @GetMapping("/queryMenuTree")
    public Result queryMenuTree(){
        return Result.success(menuService.queryMenuTree());
    }

    @GetMapping("/queryMenu")
    public Result queryMenu(){
        return Result.success(menuService.queryMenu());
    }

    @PostMapping("/save")
    public Result save(@RequestBody TpMenu tpMenu){
        try {
            menuService.save(tpMenu);
            return Result.success("添加成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return Result.fail("添加失败");
        }
    }

    @PostMapping("/update")
    public Result update(@RequestBody TpMenu tpMenu){
        try {
            menuService.update(tpMenu);
            return Result.success("修改成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return Result.fail("修改失败");
        }
    }

    @GetMapping("/delete")
    public Result update(Integer id){
        try {
            menuService.delete(id);
            return Result.success("删除成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return Result.fail("修改失败");
        }
    }

    @GetMapping("/queryById")
    public Result queryById(Integer id){
        TpMenu tpMenu = menuService.queryById(id);
        return Result.success(tpMenu);
    }


}
